package ua.org.nuos.sdms.clientgui.client.components.builders;

import com.vaadin.terminal.ExternalResource;
import com.vaadin.ui.*;
import com.vaadin.ui.themes.ChameleonTheme;
import ua.org.nuos.sdms.clientgui.client.components.factory.ButtonFactory;
import ua.org.nuos.sdms.clientgui.client.components.factory.LayoutFactory;
import ua.org.nuos.sdms.clientgui.client.controller.GlobalBoard;
import ua.org.nuos.sdms.clientgui.client.events.DetachUserFromGroupEvent;
import ua.org.nuos.sdms.clientgui.client.events.UpdateUserInGroupToLeaderEvent;
import ua.org.nuos.sdms.clientgui.server.AppProxy;
import ua.org.nuos.sdms.middle.entity.Group;
import ua.org.nuos.sdms.middle.entity.User;

/**
 * Created by IntelliJ IDEA.
 * User: dio
 * Date: 08.04.12
 * Time: 21:54
 * To change this template use File | Settings | File Templates.
 */
public class UserInfoBuilder {

    private static final String BUTTON_WIDTH = "130";
    private static final String MAILTO = "mailto:";
    private static final String LEADER = " (лидер)";

    public static Component build(User user, boolean isCurrentUserLeader) {
        Group group = AppProxy.getInstance().getModelLocator().getGroup();
        if (group == null) {
            //todo refactor (change VerticalLayout to somethin else)
            return new VerticalLayout();
        } else {
            UserInGroup.Type type = AppProxy.getInstance().getServiceLocator().getClientBean()
                    .findUserGroupType(user.getId(), group.getId());
            boolean isLeader = UserInGroup.Type.LEADER.equals(type);

            Component info = buildUserInfo(user, isLeader);
            Component buttons = buildUserActivityButtons(user, isLeader);
            HorizontalLayout content = new HorizontalLayout();
            content.setWidth("100%");
            content.addStyleName("user_info");

            content.addComponent(info);
            content.setExpandRatio(info, 1.f);
            if (isCurrentUserLeader && buttons != null) {
                content.addComponent(buttons);
                content.setComponentAlignment(buttons, Alignment.BOTTOM_RIGHT);
            }

            Panel panel = new Panel();
            panel.setWidth("100%");
            panel.setStyleName(ChameleonTheme.PANEL_LIGHT);
            panel.addStyleName("panel_user_info");
            panel.addComponent(content);

            return panel;
        }
    }

    private static Component buildUserInfo(User user, boolean isLeader) {
        String fullName = user.getFullName();
        if (isLeader) {
            fullName += LEADER;
        }
        Label name = new Label(fullName);
        name.setStyleName(ChameleonTheme.LABEL_H3);
        Link email = new Link(user.getEmail(), new ExternalResource(MAILTO + user.getEmail()));

        VerticalLayout content = new VerticalLayout();
        content.setWidth("100%");
        content.setSpacing(true);
        content.addComponent(name);
        content.addComponent(email);

        return content;
    }

    private static Component buildUserActivityButtons(User user, boolean isLeader) {
        if (isLeader) {
            return null;
        } else {
            Button leaderButton = ButtonFactory.getButton("Повысить до лидера", GlobalBoard.ID, new UpdateUserInGroupToLeaderEvent(user.getId()));
            leaderButton.setWidth(BUTTON_WIDTH);
            Button detachButton = ButtonFactory.getButton("Изгнать из группы", GlobalBoard.ID, new DetachUserFromGroupEvent(user.getId()));
            detachButton.setWidth(BUTTON_WIDTH);
            ButtonFactory.setButtonColorStyle(detachButton, ButtonFactory.Color.RED);
            VerticalLayout buttons = LayoutFactory.getVLayout(leaderButton, detachButton);
            buttons.setHeight("100%");
            buttons.setComponentAlignment(leaderButton, Alignment.TOP_CENTER);
            buttons.setComponentAlignment(detachButton, Alignment.BOTTOM_CENTER);
            buttons.setWidth(BUTTON_WIDTH);
            return buttons;
        }
    }
}
